﻿$(document).ready(function () {

    $("#btnEdit").click(function () {
        $("#divEditCate").css("display", "");
        $("#divAddNew").css("display", "none");
        $("#divresult").css("display", "none");
        $("#btnSave").css("display", "");
        $.ajax({
            url: "Category.aspx/ShowCate",
            type: "POST",
            // data: "{Name:'" + CateName + "',Description:'" + CateDesc + "',ParentId:" + ParentCate + ",IsActive:" + IsActive + ",IsDelete:false,Order:" + Order + "}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) { $("#divEditCate").html(data.d); },
            error: function () { alert("Error roi "); }
        });
    })

    $("#btnAddNew").click(function () {
        $("#divresult").css("display", "none");
        $("#divEditCate").css("display", "none");
        $("#btnSave").css("display", "none");
        $("#divAddNew").css("display", "");
    })

    //    $("select[id$='ddlCategory']").change(function () {
    //        var iCateIdAdd = $("select[id$='ddlCategory'] option:selected").val();
    //        GetSubMenu(iCateIdAdd);
    //    });

    $("#btnSave").click(function () {

        $("#tabCate tr:gt(0)").each(function () {
            var curTR = $(this);
            var sCateName;
            var sMainCateName = $("td:first", this).find("input[type='text']").val();
            var sSubCateName = $("td:eq(1)", this).find("input[type='text']").val();
            if (sMainCateName !== undefined)
                sCateName = sMainCateName;
            if (sSubCateName !== undefined)
                sCateName = sSubCateName;
            var iOrder = $("td:eq(2)", this).find("input[type='text']").val();
            var iCateId = $("td:eq(5)", this).find("input[type='hidden']").val();
            var isActive = $("td:eq(3)", this).find("input[type='checkbox']").is(':checked');
            var isDelete = $("td:eq(4)", this).find("input[type='checkbox']").is(':checked');

            $("td:eq(6)", this).find("img").attr("src", "../Images/Common/loading.gif");

            $.ajax({
                url: "Category.aspx/UpdateCate",
                type: "POST",
                data: "{CateId:" + iCateId + ",Name:'" + sCateName + "',Description:'',IsActive:" + isActive + ",IsDelete:" + isDelete + ",Order:" + iOrder + "}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) { $("td:eq(6) img:eq(0)", $(curTR)).attr("src", "../Images/Common/check.png"); },
                error: function () { $("td:eq(6)", $(curTR)).find("img").attr("src", "../Images/Common/error.png"); }
            });

        });
    });

    /*For add category*/
    $("#btnSaveAddCate").click(function () {

        $("#divresult").css("display", "");
        $("#imgstatus").attr("src", "../Images/Common/loading.gif");
        $("#spStatus").text("Loading ... ");

        var CateName = $("input[id$='txtName']").val();
        var CateDesc = $("textarea[id$='txtDesc']").val();
        var ParentCate = $("select[id$='ddlCategory'] option:selected").val();
        var IsActive = $("input[id$='ckbIsShowHomePage']").is(":checked");
        var Order = $("input[id$='txtOrder']").val();

        $.ajax({
            url: "Category.aspx/InsertCate",
            type: "POST",
            data: "{Name:'" + CateName + "',Description:'" + CateDesc + "',ParentId:" + ParentCate + ",IsActive:" + IsActive + ",IsDelete:false,Order:" + Order + "}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                $("#divresult").css("display", "");
                $("#imgstatus").attr("src", "../Images/Common/check.png");
                $("#spStatus").text("Successful").css("color","Blue");                
            },
            error: function () {
                $("#divresult").css("display", "");
                $("#imgstatus").attr("src", "../Images/Common/error.png");
                $("#spStatus").text("Ui cha, có lỗi gòi").css("color","red");
            }
        });

    });
});

